package com.one.group.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.one.group.admin.excel.SpMeterReadingExcelData;
import com.one.group.admin.excel.SpMeterReadingExcelDataReadListener;
import com.one.group.admin.mapper.SpMeterReadingdataMapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.SpMeterReadingdataAddDto;
import com.one.group.model.dto.SpMeterReadingdataQueryDto;
import com.one.group.model.entity.SpMeterReadingdata;
import com.one.group.model.vo.SpMeterReadingdataListVo;
import com.one.group.result.Result;
import com.one.group.service.SpMeterReadingdataService;
import com.one.group.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/SpMeterReadingdataController")
@RestController
@Tag(name = "抄表数据测试相关接口")
@Slf4j
public class SpMeterReadingdataController {
    @Autowired
    private SpMeterReadingdataService spMeterReadingdataService;
    @Autowired
    private SpMeterReadingdataMapper spMeterReadingdataMapper;
    @PostMapping("/import")
    @Operation(summary = "导入数据")
    public Result importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), SpMeterReadingExcelData.class,
                    new SpMeterReadingExcelDataReadListener(spMeterReadingdataService, spMeterReadingdataMapper)).sheet().doRead();
            return ResultUtil.success();
        } catch (Exception e) {
            log.error("导入数据异常", e);
            return ResultUtil.error(ErrorCode.ADD_ERROR);
        }
    }
    @PostMapping("/queryList")
    @Operation(summary = "查询数据")
    public Result queryList(@RequestBody SpMeterReadingdataQueryDto spMeterReadingdataQueryDto) {
        //构建分页条件
        PageHelper.startPage(spMeterReadingdataQueryDto.getPage(), spMeterReadingdataQueryDto.getLimit());
        List<SpMeterReadingdataListVo> list = spMeterReadingdataService.queryList(spMeterReadingdataQueryDto);
        PageInfo<SpMeterReadingdataListVo> pageInfo = new PageInfo<>(list);
        return ResultUtil.success(pageInfo);
    }
    @PostMapping("/add")
    @Operation(summary = " 添加数据")
    public Result add(@RequestBody SpMeterReadingdataAddDto spMeterReadingdataAddDto) {
        boolean result = spMeterReadingdataService.addMeterReadingdata(spMeterReadingdataAddDto);
        if (result) {
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.ADD_ERROR);
    }
    @GetMapping("/export")
    @Operation(summary = "导出数据")
    public void export(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("抄表数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SpMeterReadingdataListVo> list = spMeterReadingdataService.myExcelExport();
        List<SpMeterReadingExcelData> spMeterReadingExcelDataList = new ArrayList<>();
        for (SpMeterReadingdataListVo data : list) {
            SpMeterReadingExcelData spMeterReadingExcelData = getSpMeterReadingExcelData(data);
            spMeterReadingExcelDataList.add(spMeterReadingExcelData);
         }
        EasyExcel.write(response.getOutputStream(), SpMeterReadingExcelData.class).sheet("权限数据").doWrite(spMeterReadingExcelDataList);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto) {
        boolean result = spMeterReadingdataService.removeByIds(baseDeleteDto.getIds());
        if (result) {
            return ResultUtil.success();
        }
        return ResultUtil.error(ErrorCode.DELETE_ERROR);
    }

    private static SpMeterReadingExcelData getSpMeterReadingExcelData(SpMeterReadingdataListVo data) {
        SpMeterReadingExcelData spMeterReadingExcelData = new SpMeterReadingExcelData();
        spMeterReadingExcelData.setCellName(data.getCellName());
        spMeterReadingExcelData.setBuildingTypeName(data.getBuildingTypeName());
        spMeterReadingExcelData.setBuildingName(data.getBuildingName());
        spMeterReadingExcelData.setHouseNum(data.getHouseNum());
        spMeterReadingExcelData.setChargestandardName(data.getChargestandardName());
        spMeterReadingExcelData.setCurrentMonth(data.getCurrentMonth().toString());
        spMeterReadingExcelData.setStartingDegree(data.getStartingDegree().toString());
        spMeterReadingExcelData.setEndDegree(data.getEndDegree().toString());
        spMeterReadingExcelData.setMagnification(data.getMagnification().toString());
        return spMeterReadingExcelData;
    }

}
